package com.sky.service.user.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.mapper.user.ShoppingCartMapper;
import com.sky.service.user.ShoppingCartService;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service("userShoppingCartService")
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private static final Logger log = LoggerFactory.getLogger(ShoppingCartServiceImpl.class);
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> select() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.select(shoppingCart);
        return shoppingCartList;
    }

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void insert(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        // 查询是否存在相同的购物车
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.select(shoppingCart);


        // 查询出来的购物车第一个商品为空，执行插入
        if (shoppingCartList.isEmpty()) {
            // 如果是菜品
            if (shoppingCart.getDishId() != null) {
                Dish dish = dishMapper.selectById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }
            // 如果是套餐
            else if (shoppingCart.getSetmealId() != null) {
                Setmeal setmeal = setmealMapper.selectById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            log.info("新增菜品或套餐");
            shoppingCartMapper.insert(shoppingCart);
        } else {
            // 更新操作，数量+1
            ShoppingCart existingCart = shoppingCartList.get(0);
            existingCart.setNumber(existingCart.getNumber() + 1);
            log.info("更新购物车数量");
            shoppingCartMapper.update(existingCart);

        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteAll() {

        shoppingCartMapper.deleteAll(BaseContext.getCurrentId());
    }

    /**
     * 购物车删除物品
     *
     * @param shoppingCartDto
     */
    @Override
    public void deleteById(ShoppingCartDTO shoppingCartDto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDto, shoppingCart);
        Long currentId = BaseContext.getCurrentId();

        shoppingCart.setUserId(currentId);
        // 执行查询操作
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.select(shoppingCart);
        // 根据dto查询出来的对应的购物单
        ShoppingCart cart = shoppingCartList.get(0);

        // 当物品数量大于2，执行更新操作number - 1
        if (cart.getNumber() > 1){
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.update(cart);
        }else{
            // 当物品数量等于1， 执行删除操作
            shoppingCartMapper.deleteById(cart);
        }



    }

}
